/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2019/1/19
 * Time: 23:41
 */
$(function () {

    $('.input-group.date').datepicker({
        todayBtn: false,
        todayHighlight: true,
        keyboardNavigation: false,
        forceParse: false,
        calendarWeeks: true,
        autoclose: true,
        format: "yyyy-mm-dd",
        language: "cn",
    });

    sendAjax('/v1/user/file.json', 'get', {}, function (json) {
        var role_pic = 'teacher.png';
        switch (json.body.role) {
            case 1: role_pic = 'teacher.png';break;
            case 2: role_pic = 'scientist.png';break;
            case 3: role_pic = 'manager.png';break;
            case 4: role_pic = 'assistant.png';break;
            case 5: role_pic = 'student.png';break;
        }
        $('.img-circle').attr('src', '/img/'+role_pic);
        $('.user-realname').text(json.body.realname);
        $('.user-department-id').val(json.body.department_id);
        $('.user-department').text(json.body.department_name);
        $('.user-number').text(json.body.number);
        $('.user-birthday').text(json.body.birthday);
        $('.user-join-time').text(json.body.join_party_at);
        $('.user-mobile').text(json.body.mobile);
        $('.user-role-code').val(json.body.role);
        $('.user-role').text(json.body.role_name);
        $('.user-education-code').val(json.body.education_code);
        $('.user-education').text(json.body.education_name);
        $('.user-score').text(json.body.score);
        var star = '';
        var count = 0;
        if (json.body.score >= 90) {
            count = 5;
        } else if (json.body.score >= 60) {
            count = 3;
        } else {
            count = 0;
        }
        for (var i = 0; i < count; i ++) {
            star += '<i class="fa fa-star"></i>';
        }
        var level = getLevelName(json.body.level);
        $('.user-star').html(level);
        $('.user-summary-text').val(json.body.summary);
    });

    var url = '/v1/user/advice.json';
    var data = {
        year : 0,
        page : 1,
        page_size : 10
    };
    getDatas(url, data);

    $('.select-year').on('change', function () {
        data.year = $(this).val();
        data.page = 1;
        getDatas(url, data);

    });

    $(document).on('click', '.page-list a', function(){
        data.year = $('.select-year').val();
        data.page = $(this).attr('data-id');
        getDatas(url, data);
    });

    $('#normal-dialog').on('show.bs.modal', function () {
        $(this).find('.modal-header .modal-title').text('查看自我评价');
        $(this).find('.modal-body').html('<div>'+$('.user-summary-text').val()+'</div>');
        $(this).find('.modal-footer .btn-primary').addClass('hidden');
    }).on('hide.bs.modal', function () {
        $(this).find('.modal-header .modal-title').text('普通弹窗');
        $(this).find('.modal-body').html('');
        $(this).find('.modal-footer .btn-primary').removeClass('hidden');
    });
    
    $('.bind-user-edit-file').on('click', function () {
        var modal_body = $('#edit-user-file').find('.modal-body');
        modal_body.find('.real-name').text($('.user-realname').text());
        modal_body.find('.number').text($('.user-number').text());
        if ($('.user-education-code').val() > 0) {
            modal_body.find('.education-code').val($('.user-education-code').val());
            modal_body.find('.education-code option').each(function () {
                if ($(this).val() == $('.user-education-code').val()) {
                    $(this).attr('selected', 'selected');
                }
            });
        }
        modal_body.find('.birthday').val($('.user-birthday').text());
        modal_body.find('.join-time').val($('.user-join-time').text());
        modal_body.find('.mobile').val($('.user-mobile').text());
        if ($('.user-role-code').val() > 0) {
            modal_body.find('.role-code').val($('.user-role-code').val());
            modal_body.find('.role-code option').each(function () {
                if ($(this).val() == $('.user-role-code').val()) {
                    $(this).attr('selected', 'selected');
                }
            });
        }
        sendAjax('/v1/user/department.json', 'get', {}, function (json) {
            var option = '';
            var select = '';
            $.each(json.body, function (n, v) {
                if ($('.user-department-id').val() == v.id) {
                    modal_body.find('.department-id').val(v.id);
                    select = 'selected="selected"';
                } else {
                    select = '';
                }
                option += '<option value="'+v.id+'" '+select+'>'+v.name+'</option>';
            });
            modal_body.find('.department-id').html(option);
        });
        modal_body.find('.summary').val($('.user-summary-text').val());
        $('#edit-user-file').modal('show');
    });

    $('#edit-user-file .dialog-btn-save').on('click', function () {
        var params = {
            education_code: $('.education-code').val(),
            birthday: $('.birthday').val(),
            mobile: $('.mobile').val(),
            join_party_at: $('.join-time').val(),
            department_id: $('.department-id').val(),
            role: $('.role').val(),
            summary: $('.summary').val()
        };
        if (checkForm(params)) {
            saveData('/v1/user/update', {params: params}, '', '更新成功');
        }
    });

});



function getDatas(url, data) {
    sendAjax(url, 'get', data, function (json) {
        var html = '';
        var page_html = '';
        if (json.rows.length > 0) {
            $.each(json.rows, function (n, v) {
                html += '<div class="row"><div class="col-xs-12 data-detail">';
                html += '<span>党支部名称：</span><span class="department-name">'+v.department_name+'</span>';
                html += '<span>&nbsp;时间：</span><span class="create-time">'+v.created_at+'</span></div>';
                html += '<div class="col-xs-12"><strong>意见内容：</strong></div>';
                html += '<div class="col-xs-12"><p class="advice-content">'+v.content+'</p></div></div>';
            });
            page_html = pageList(data.page, data.page_size, json.total, json.pages, json.rows.length);
        }
        $('.data-list').html(html);
        $('.page-list').html(page_html);
    });
}

function checkForm(data) {
    if (data.education_code == '' || data.education_code == undefined) {
        alert('请选择学历');
        return false;
    }
    if (data.birthday == '') {
        alert('请填写出生日期');
        return false;
    }
    if (data.mobile == '') {
        alert('请填写联系方式');
        return false;
    }
    if (data.join_party_at == '') {
        alert('请填写入党时间');
        return false;
    }
    if (data.department_id == '') {
        alert('请选择所属支部');
        return false;
    }
    if (data.role == '') {
        alert('请选择角色');
        return false;
    }
    return true;
}